﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TYBase
{
    public static class DataExtend
    {
        public static int[] ObjAsIntArray(this object obj)
        {
            if (obj is bool bool_val)
            {
                return new[] { bool_val ? 1 : 0 };
            }
            if (obj is int int_val)
            {
                return new[] { int_val };
            }
            if (obj is short short_val)
            {
                return new[] { (int)short_val };
            }
            if (obj is ushort ushort_val)
            {
                return new[] { (int)ushort_val };
            }
            if (obj is uint uint_val)
            {
                return new[] { (int)uint_val };
            }

            int[] arr = null;
            if (obj is bool[] bools_val)
            {
                arr = new int[bools_val.Length];
                for (int i = 0; i < bools_val.Length; i++)
                {
                    arr[i] = bools_val[i] ? 1 : 0;
                }
                return arr;
            }


            if (obj is int[] ints_val)
            {
                arr = new int[ints_val.Length];
                for (int i = 0; i < ints_val.Length; i++)
                {
                    arr[i] = ints_val[i];
                }
                return arr;
            }
            if (obj is short[] shorts_val)
            {
                arr = new int[shorts_val.Length];
                for (int i = 0; i < shorts_val.Length; i++)
                {
                    arr[i] = shorts_val[i];
                }
                return arr;
            }
            if (obj is ushort[] ushorts_val)
            {
                arr = new int[ushorts_val.Length];
                for (int i = 0; i < ushorts_val.Length; i++)
                {
                    arr[i] = ushorts_val[i];
                }
                return arr;
            }
            if (obj is uint[] uints_val)
            {
                arr = new int[uints_val.Length];
                for (int i = 0; i < uints_val.Length; i++)
                {
                    arr[i] = (int)uints_val[i];
                }
                return arr;
            }
            return null;
        }

        public static ushort Asushort(this string str, ushort ret = 0)
        {
            if (str.Null())
                return ret;
            if (ushort.TryParse(str, out ushort data))
            {
                return data;
            }
            return ret;
        }
        public static string IfAs(this int data, int IfValue, string res)
        {
            if (data == IfValue)
            {
                return res;
            }
            return data.ToString();
        }

        public static int IfNotAs(this int data, int minVal, int maxValue, int res)
        {
            if (data >= minVal && data <= maxValue)
            {
                return data;
            }
            return res;
        }
    }
}
